define(["jquery"],function($){
	var quest = {};

	/**
	 * 数据处理(若不需要数据处理，可以不写)
	 */
    quest.formatData = function(obj){
    	if( obj.list && $.isArray(obj.list) && obj.list.length > 0 ){
    		for(var i = 0; i < obj.list.length; i++){
    			if( typeof obj.list[i] == "string" ){
    				var str = obj.list[i];
    				obj.list[i] = {};
    				obj.list[i].key = str;
    				obj.list[i].text = str;
    			}
    		}
    		obj.listStr = JSON.stringify(obj.list);
    	}
		return obj;
	}

	/**
	 * tpl模板
	 */
	quest.tpl = function(){
		return 	'<script id="tpl_question_select" type="text/html">'+
				'	<div {{if relationId}}relationId="{{relationId}}"{{/if}} class="qaCard form-group form-group-sm col-sm-{{length}} col-md-{{length}} grid-label-{{labelLength}}">'+
				'		<label class="control-label {{align}}">{{label}}</label>'+
				'   	<div class="control-input">'+
				'			{{if status=="default"}}'+
				'				<select class="form-control validatebox" name="{{name}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" data-dataUnit="{{dataUnit}}" data-validate="{{validate}}" data-attention="{{attention}}">'+
				'					{{each list as item i}}'+
				'   					<option value="{{item.key}}">{{item.text}}</option>'+
				'					{{/each}}'+
				'				</select>'+
				'			{{else if status=="readonly"}}'+
				'				<p class="form-control-static validatebox" name="{{name}}" data-list="{{listStr}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" data-dataUnit="{{dataUnit}}" data-validate="{{validate}}" data-attention="{{attention}}"></p>'+
				'			{{else if status=="disabled"}}'+
				'				<select class="form-control validatebox" name="{{name}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" data-dataUnit="{{dataUnit}}" data-validate="{{validate}}" data-attention="{{attention}}" disabled>'+
				'					{{each list as item i}}'+
				'   					<option value="{{item.key}}">{{item.text}}</option>'+
				'					{{/each}}'+
				'				</select>'+
				'			{{/if}}'+
				'  		</div>'+
				'	</div>'+
				'</script>';
	}

	/**
	 * 赋值
	 */
	quest.setValue = function($dom,value){
		if( value ){
			if( $dom.hasClass("form-control-static") ){
				var list = JSON.parse( $dom.attr("data-list") );
				for (var i = 0; i < list.length; i++) {
					if( list[i].key == value.quesValue ){
						$dom.html( list[i].text );
						break;
					}
				}
			}else{
				$dom.val(value.quesValue);
			}
		}
	}

	/**
	 * 获取value（保存时，获取每个dom对应的答案）
	 */
	quest.getValue = function($dom,name){
		var value = {};
		if( $dom.hasClass("form-control-static") ){
			var list = JSON.parse( $dom.attr("data-list") );
			for (var i = 0; i < list.length; i++) {
				if( list[i].text == $dom.html() ){
					var val = list[i].key;
					break;
				} 
			}
		}else{
			var val = $dom.val();
		}
		value.quesValue = val;
		value.quesId = $dom.attr("data-quesId");
		value.deCode = $dom.attr("data-deCode");
		value.innDeCode = $dom.attr("data-innDeCode");
		var dataUnit = $dom.attr("data-dataUnit");
		if( dataUnit ) value.dataUnit = dataUnit;
		return value;
	}
    
	return quest;
});